unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, DB, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls, DBGrids, Menus, Buttons, IBQuery, IBDatabase, ZConnection, ZDataset, IniFiles;

type
  { TForm1 }
  TForm1 = class(TForm)
    ibdb: TIBDatabase;
    ibq: TIBQuery;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    MenuItem10: TMenuItem;
    MenuItem11: TMenuItem;
    MenuItem12: TMenuItem;
    MenuItem13: TMenuItem;
    MenuItem14: TMenuItem;
    MenuItem15: TMenuItem;
    MenuItem16: TMenuItem;
    MenuItem17: TMenuItem;
    MenuItem18: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    MenuItem7: TMenuItem;
    MenuItem8: TMenuItem;
    MenuItem9: TMenuItem;
    SpeedButton1: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure MenuItem10Click(Sender: TObject);
    procedure MenuItem12Click(Sender: TObject);
    procedure MenuItem13Click(Sender: TObject);
    procedure MenuItem14Click(Sender: TObject);
    procedure MenuItem15Click(Sender: TObject);
    procedure MenuItem16Click(Sender: TObject);
    procedure MenuItem17Click(Sender: TObject);
    procedure MenuItem18Click(Sender: TObject);
    procedure MenuItem3Click(Sender: TObject);
    procedure MenuItem4Click(Sender: TObject);
    procedure MenuItem5Click(Sender: TObject);
    procedure MenuItem9Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    current_ean: string; // aktualnie klikniety ean w gornym gridzie
    first_view: boolean;

    procedure LoadConfig;
    procedure SaveConfig;

    function DBConnect: boolean;

    procedure InitDBComponents;
  end;

type
  TConfig = record
    dbpath    : string;
    user      : string;
    pass      : string;
    ip_addr   : string;
    kodowanie : string;
    urz_zew   : string;
  end;


var
  cfg: TConfig;

function AddSlash(txt: string): string;


var
  Form1: TForm1;

implementation

uses
  unit2, unit4, Unit7, Unit9, Unit10, Unit11, Unit13, Unit16, Unit18, Unit19, unit20,
  unit21, // kontrahenci
  unit23; // synchornizacja

{$R *.lfm}

{ TForm1 }



// System -> Parametry systemu
procedure TForm1.MenuItem10Click(Sender: TObject);
begin
  form2.ed_pass.Text      := cfg.pass;
  form2.ed_path_baza.Text := cfg.dbpath;
  form2.ed_srv_addr.Text  := cfg.ip_addr;
  form2.ed_stand.Text     := cfg.kodowanie;
  form2.ed_user.Text      := cfg.user;
  form2.ed_urz_zew.Text   := cfg.urz_zew;

  form2.ShowModal;

  cfg.pass      := form2.ed_pass.Text;
  cfg.dbpath    := form2.ed_path_baza.Text;
  cfg.ip_addr   := form2.ed_srv_addr.Text;
  cfg.kodowanie := form2.ed_stand.Text;
  cfg.user      := form2.ed_user.Text;
  if copy(form2.ed_urz_zew.Text,length(form2.ed_urz_zew.Text),1) <> '\' then form2.ed_urz_zew.Text:=form2.ed_urz_zew.Text+'\';
  cfg.urz_zew   := form2.ed_urz_zew.Text;

  SaveConfig;
end;

procedure TForm1.MenuItem12Click(Sender: TObject);
begin
  Form9.FillComboBoxFilters();
  Form9.ShowModal;
end;

procedure TForm1.MenuItem13Click(Sender: TObject);
begin
  form16.Show;
end;

procedure TForm1.MenuItem14Click(Sender: TObject);
begin
  form18.Show;
end;

procedure TForm1.MenuItem15Click(Sender: TObject);
begin
  form21.Show;
end;

procedure TForm1.MenuItem16Click(Sender: TObject);
begin
  form23.urz_zew:=cfg.urz_zew;
  form23.ShowModal;
end;

// nowy dokument przychodowy
procedure TForm1.MenuItem17Click(Sender: TObject);
begin
  // odblokowanie wybranych kontrolek, majacych znaczrnienie
  form20.ComboBox1.Enabled:=true; // odblokowanie typu dokumentu
  form20.Edit2.Enabled:=true;     // odblokowanie kontrahenta
  form20.Button1.Enabled:=true;   // odblokowanie kontrahenta
  form20.naglowki_idx:=-1;

  form20.dok_type:=dtPrzychod;
  form20.FillComboBox;
  form20.showmodal;
end;

// nowy dokument rozchodowy
procedure TForm1.MenuItem18Click(Sender: TObject);
begin
  // odblokowanie wybranych kontrolek, majacych znaczrnienie
  form20.ComboBox1.Enabled:=true; // odblokowanie typu dokumentu
  form20.Edit2.Enabled:=true;     // odblokowanie kontrahenta
  form20.Button1.Enabled:=true;   // odblokowanie kontrahenta
  form20.naglowki_idx:=-1;

  form20.dok_type:=dtRozchod;
  form20.FillComboBox;
  form20.showmodal;
end;

procedure TForm1.MenuItem3Click(Sender: TObject);
begin
  form11.Show;
end;

// definicje dokumentów
procedure TForm1.MenuItem4Click(Sender: TObject);
begin
  form7.Show;
end;

// kartoteka asortymentu
procedure TForm1.MenuItem5Click(Sender: TObject);
begin
  // wylaczeenie przycikow uzywanych podczas pisania dokumentu
  form13.bt_anuluj.Visible := False;
  form13.bt_anuluj.Enabled := False;
  form13.bt_ok.Visible := False;
  form13.bt_ok.Enabled := False;

  // pokazanie przynisku koniec
  form13.bt_zamknij.Enabled := True;
  form13.bt_zamknij.Visible := True;

  form13.Show;
end;

// System -> Urzytkownicy
procedure TForm1.MenuItem9Click(Sender: TObject);
begin
  form4.Show;
end;

// przycisk obecnosci
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  form10.ShowModal;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  LoadConfig;

  if not DBConnect then
  begin
    ShowMessage('Nie moge podlaczyć sie do bazy. Praca nie mozliwa');
    exit;
  end;
  first_view := True;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  if first_view then
  begin
    form1.InitDBComponents;
    first_view := False;
  end;
end;


procedure TForm1.LoadConfig;
var
  ini : TIniFile;
begin
  ini:=TIniFile.Create('.\config.ini');
  cfg.dbpath   :=ini.ReadString('main','dbpath','');
  cfg.user     :=ini.ReadString('main','user','');
  cfg.pass     :=ini.ReadString('main','pass','');
  cfg.ip_addr  :=ini.ReadString('main','ip_addr','');
  cfg.kodowanie:=ini.ReadString('main','kodowanie','');
  cfg.urz_zew  :=ini.ReadString('main','urz_zew','');
  ini.Free;
end;

procedure TForm1.SaveConfig;
var
  ini : TIniFile;
begin
  ini:=TIniFile.Create('.\config.ini');
  ini.WriteString('main','dbpath',cfg.dbpath);
  ini.WriteString('main','user',cfg.user);
  ini.WriteString('main','pass',cfg.pass);
  ini.WriteString('main','ip_addr',cfg.ip_addr);
  ini.WriteString('main','kodowanie',cfg.kodowanie);
  ini.WriteString('main','urz_zew',cfg.urz_zew);
  ini.free;
end;

function TForm1.DBConnect: boolean;
begin
  ibdb.Params.Clear;
  ibdb.Params.Add('lc_ctype=' + cfg.kodowanie);
  ibdb.Params.Add('user_name=' + cfg.user);
  ibdb.Params.Add('password=' + cfg.pass);
  ibdb.DatabaseName := cfg.dbpath;
  try
    ibdb.Open;
  except
    DBConnect := False;
  end;
end;

procedure TForm1.InitDBComponents;
begin
  form2.ibq.Database    := form1.ibq.Database;
  form2.ibsql.Database  := form1.ibq.Database;
  form2.ibproc.Database := form1.ibq.Database;

  form9.ibq1.Database := form1.ibdb;

  form16.ibq1.Database := form1.ibdb;
  form16.ibq2.Database := form1.ibdb;

  form18.ibq1.Database := form1.ibdb;
  form18.ibq2.Database := form1.ibdb;

  form11.ibq1.Database := form1.ibdb;
  form11.ibq2.Database := form1.ibdb;

  form7.ibq1.Database := form1.ibdb;
  form7.ibq2.Database := form1.ibdb;

  form13.ibq1.Database := form1.ibdb;
  form13.ibq2.Database := form1.ibdb;

  form4.ibq1.Database := form1.ibdb;
  form4.ibq2.Database := form1.ibdb;

  form10.ibq.Database := form1.ibdb;

  form20.ibq1.Database:=form1.ibdb;
  form20.ibq2.Database:=form1.ibdb;

  form23.ibq1.Database:=form1.ibdb;

  form19.ibq1.Database:=form1.ibdb;
  unit19.ibq1:=form19.ibq1;
  unit19.ibt1:=form19.ibt1;
end;


function AddSlash(txt: string): string;
begin
  if txt[length(txt)] <> '\' then
    txt := txt + '\';
  AddSlash := txt;
end;


end.

{
function TForm1.LoadConfig: boolean;
var
  f: file of TConfig;
  b: TConfig;

begin
  LoadConfig := False;
  AssignFile(f, '.\config.cfg');
  {$I-}
  reset(f);
  if IOResult <> 0 then
    exit;
  Read(f, b);
  if IOResult <> 0 then
  begin
    CloseFile(f);
    exit;
  end;
  CloseFile(f);
  {$I+}
  if (b.sgn1 <> 'asdf') or (b.sgn2 <> 'qwer') or (b.sgn3 <> 'zxcv') then
    exit; // kontrola sygnatur pliku konfiguracji
  cfg := b;

  LoadConfig := True;

end;

function TForm1.SaveConfig: boolean;
var
  f: file of TConfig;
begin
  SaveConfig := False;

  cfg.pass := form2.ed_pass.Text;
  cfg.dbpath := form2.ed_path_baza.Text;
  cfg.ip_addr := form2.ed_srv_addr.Text;
//  cfg.fb_ver := form2.ed_srv_ver.Text;
  cfg.kodowanie := form2.ed_stand.Text;
  cfg.user := form2.ed_user.Text;

  AssignFile(f, '.\config.cfg');
  {$I-}
  rewrite(f);
  if IOResult <> 0 then
    exit;
  cfg.sgn1 := 'asdf';
  cfg.sgn2 := 'qwer';
  cfg.sgn3 := 'zxcv';
  Write(f, cfg);
  if IOResult <> 0 then
    exit;
  CloseFile(f);
  {$I+}
  SaveConfig := True;
end;

}
